package com.test.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.test.classs.hd_student;
import com.test.connection_hive.hive_SQL;
import com.test.page.PageInfo;

public class deal_hd_student {
	
	/**
	 * @author qinzhen
	 * @time 2015/3/14
	 * @param stmt
	 * @param pageInfo
	 * @param fieldName
	 * @param value
	 * @return
	 * @throws SQLException
	 * @throws Exception
	 * @function 根据字段名称和值，查询学生信息    ,  如学号id，身份证id，专业id
	 */
	public static List<hd_student> selectHd_studentFromFieldName(Statement stmt,PageInfo pageInfo,
			String fieldName, String value) throws SQLException, Exception {

		String querySQL = "select * from studentmanagesystem.hd_student where "
				+ fieldName + "='" + value + "\'";
		
//		if (pageInfo != null) {
//			querySQL += ("where cid>=" + pageInfo.getCurrentPageNow()
//					+ " and cid<= " + pageInfo.getPageSize());
//			pageInfo.setTotalResult(getCount("id", "hd_student"));
//
//		}
		List<hd_student> studentList = new ArrayList<hd_student>();
		ResultSet res = stmt.executeQuery(querySQL);

		while (res.next()) {
			hd_student student = new hd_student();

			byte[] b = res.getString("address").getBytes("utf-8");// 缂栫爜
			String sa = new String(b, "gbk");//
			System.out.println(sa + " m  = " + res.getString("address"));

			student.setCid(res.getString("cid"));
			student.setId(res.getString("id"));
			student.setAddress(res.getString("address"));
			student.setBirthday(res.getString("birthday"));
			student.setCollege(res.getString("college"));
			student.setEmail(res.getString("email"));
			student.setEndschoolYear(res.getString("endschoolyear"));
			student.setIdCard(res.getString("idcard"));
			student.setIsDelete(res.getString("isdelete"));
			student.setMsg(res.getString("msg"));
			student.setName(res.getString("name"));
			student.setNation(res.getString("nation"));
			student.setPassWord(res.getString("password"));
			student.setQQ(res.getString("qq"));
			student.setSex(res.getString("sex"));
			student.setStartSchoolYear(res.getString("startschoolyear"));
			student.setState(res.getString("state"));
			student.setStuId(res.getString("stuid"));
			student.setTrainingLevel(res.getString("traininglevel"));
			student.setPhone(res.getString("phone"));
			student.setPic(res.getString("pic"));
			student.setPolitical(res.getString("political"));
			student.setProfessionalId(res.getString("professionalid"));
			studentList.add(student);
		}
		return studentList;
	}

	/**
	 * @author qinzhe
	 * @time 2015/3/14
	 * @param stmt
	 * @param pageInfo
	 * @return List<hd_student>
	 * @throws SQLException
	 * @throws Exception
	 * @function 分页查询所有学生信息数据
	 */
	public static List<hd_student> selectAllHd_student(Statement stmt,
			PageInfo pageInfo) throws SQLException, Exception {
//		System.out.println("pageInfo.getCurrentPageNow()="
//				+ pageInfo.getCurrentPageNow() + " "
//				+ "pageInfo.getPageSize()=" + pageInfo.getPageSize());
		// querySQL="select * from studentmanagesystem.hd_student where name='"+loginname+"\'";
		String querySQL = "select * from studentmanagesystem.hd_student ";
		if (pageInfo != null) {
			querySQL += ("where cid>=" + pageInfo.getCurrentPageNow()
					+ " and cid<= " + pageInfo.getPageSize());
			pageInfo.setTotalResult(hive_SQL.getCount("id", "hd_student"));

		}
		List<hd_student> studentList = new ArrayList<hd_student>();
		ResultSet res = stmt.executeQuery(querySQL);
		while (res.next()) {
			hd_student student = new hd_student();
			byte[] b = res.getString("address").getBytes("iso-8859-1");// 缂栫爜
			String sa = new String(b, "gbk");//
			System.out.println(sa + " m  = " + res.getString("address"));
			student.setCid(res.getString("cid"));
			student.setId(res.getString("id"));
			student.setAddress(res.getString("address"));
			student.setBirthday(res.getString("birthday"));
			student.setCollege(res.getString("college"));
			student.setEmail(res.getString("email"));
			student.setEndschoolYear(res.getString("endschoolyear"));
			student.setIdCard(res.getString("idcard"));
			student.setIsDelete(res.getString("isdelete"));
			student.setMsg(res.getString("msg"));
			student.setName(res.getString("name"));
			student.setNation(res.getString("nation"));
			student.setPassWord(res.getString("password"));
			student.setQQ(res.getString("qq"));
			student.setSex(res.getString("sex"));
			student.setStartSchoolYear(res.getString("startschoolyear"));
			student.setState(res.getString("state"));
			student.setStuId(res.getString("stuid"));
			student.setTrainingLevel(res.getString("traininglevel"));
			student.setPhone(res.getString("phone"));
			student.setPic(res.getString("pic"));
			student.setPolitical(res.getString("political"));
			student.setProfessionalId(res.getString("professionalid"));
			studentList.add(student);
		}
		return studentList;
	}
}
